package br.com.lh.urabuilder.helper
{
	import mx.containers.Canvas;
	import mx.core.UIComponent;
	
	public class ScrollHelper
	{
		public var parentComponent:Canvas;
		private var fakeComponent:UIComponent = null;
		private var yLowerLimit:Number;
		private var yUpperLimit:Number;
		private var xRightLimit:Number;
		private var xLeftLimit:Number;
		
		private const BASE_SCROLL_INCREMENT:int = 2;
		private const DELTA_PERCENTUAL:Number = 0.2;
		
		
		public function ScrollHelper(parentComponent:Canvas){
			this.parentComponent = parentComponent;
			this.yLowerLimit = parentComponent.height * 0.85;
			this.yUpperLimit = parentComponent.height * 0.10;
			this.xLeftLimit = parentComponent.width * 0.1;
			this.xRightLimit = parentComponent.width * 0.85;
		}
		
		public function performScrollIfNeeded(imageX:Number,imageY:Number):void{
			if (imageX > xRightLimit){
				repositionScroll(calculateScrollIncrement(imageX,xRightLimit),false);
			}else if (imageX < xLeftLimit){
				repositionScroll(calculateScrollIncrement(imageX,xLeftLimit),false);
			}
			if (imageY > yLowerLimit){
				repositionScroll(calculateScrollIncrement(imageY,yLowerLimit),true);
			}else if (imageY < yUpperLimit){
				repositionScroll(calculateScrollIncrement(imageY,yUpperLimit),true);
			}
			
		}
		
		private function calculateScrollIncrement(mouse:Number,limit:Number):int{
			var delta:int = mouse - limit;
			return delta* DELTA_PERCENTUAL * BASE_SCROLL_INCREMENT;
		}
		
		        
        public function terminateScroll(maxXCreated:Number =-1,maxYCreated:Number = -1):void{
        	if (fakeComponent !=null){
			 	parentComponent.removeChild(fakeComponent);
			 	fakeComponent = null;
			}
        }
		
		private function scrollBarIsNotCreated(yAxis:Boolean):Boolean{
			if (yAxis){
				return fakeComponent !=null || parentComponent.maxVerticalScrollPosition <= parentComponent.verticalScrollPosition;
			}else{
				return fakeComponent !=null || parentComponent.maxHorizontalScrollPosition <= parentComponent.horizontalScrollPosition;
			}
		}
		
        
        private function repositionScroll(scrollIncrement:int,yAxis:Boolean):void{
          	if (scrollBarIsNotCreated(yAxis) && scrollIncrement >0){
	            terminateScroll();
	            fakeComponent= new UIComponent();
	            if (yAxis){
					fakeComponent.y = parentComponent.height + parentComponent.maxVerticalScrollPosition + scrollIncrement;
					fakeComponent.x = 0;
					parentComponent.addChild(fakeComponent);
					parentComponent.verticalScrollPosition = parentComponent.maxVerticalScrollPosition;
	            }else{
		            fakeComponent.x = parentComponent.width + parentComponent.maxHorizontalScrollPosition + scrollIncrement;
					fakeComponent.y= 0;
					parentComponent.addChild(fakeComponent);
					parentComponent.horizontalScrollPosition= parentComponent.maxHorizontalScrollPosition;
	            }
          	}else{
          		if (yAxis){
          			parentComponent.verticalScrollPosition += scrollIncrement;
          		}else{
          			parentComponent.horizontalScrollPosition += scrollIncrement;
          		}
          		
          	}
        }
        
        

        

	}
}